﻿using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Newtonsoft.Json;
using ParamSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Menu;
using xXYZ = Autodesk.Revit.DB.XYZ;

namespace RevitAddin
{
    public partial class mainForm : System.Windows.Forms.Form
    {
        private TFCodeCMD TFCodeCMD = null;
        private ExternalEvent TFCodeCMDEvent = null;

        private BYQCodeCMD BYQCodeCMD = null;
        private ExternalEvent BYQCodeCMDEvent = null;

        private YLCCodeCMD YLCCodeCMD = null;
        private ExternalEvent YLCCodeCMDEvent = null;

        private TestCMD TestCMD = null;
        private ExternalEvent TestCMDEvent = null;

        private ZJCCodeCMD ZJCCodeCMD = null;
        private ExternalEvent ZJCCodeCMDEvent = null;

        private JGCodeCMD JGCodeCMD = null;
        private ExternalEvent JGCodeCMDEvent = null;

        private WidthCodeCMD WidthCodeCMD = null;
        private ExternalEvent WidthCodeCMDEvent = null;

        private FileCodeCMD FileCodeCMD = null;
        private ExternalEvent FileCodeCMDEvent = null;
        public mainForm()
        {
            InitializeComponent();
            TFCodeCMD = new TFCodeCMD();
            TFCodeCMDEvent = ExternalEvent.Create(TFCodeCMD);

            BYQCodeCMD = new BYQCodeCMD();
            BYQCodeCMDEvent = ExternalEvent.Create(BYQCodeCMD);

            YLCCodeCMD = new YLCCodeCMD();
            YLCCodeCMDEvent = ExternalEvent.Create(YLCCodeCMD);

            TestCMD = new TestCMD();
            TestCMDEvent = ExternalEvent.Create(TestCMD);

            ZJCCodeCMD = new ZJCCodeCMD();
            ZJCCodeCMDEvent = ExternalEvent.Create(ZJCCodeCMD);

            JGCodeCMD = new JGCodeCMD();
            JGCodeCMDEvent = ExternalEvent.Create(JGCodeCMD);

            WidthCodeCMD = new WidthCodeCMD();
            WidthCodeCMDEvent = ExternalEvent.Create(WidthCodeCMD);

            FileCodeCMD = new FileCodeCMD();
            FileCodeCMDEvent = ExternalEvent.Create(FileCodeCMD);

            this.Load += MainForm_Load;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = ParamManager.getParamValue(globalVar.uiParam, "windowTitle", "根据模版数据提取布线要素");

            this.groupBox1.Text = ParamManager.getParamValue(globalVar.uiParam, "firstGroupBoxTitle", "第一步：导入设备清单");
            this.groupBox3.Text = ParamManager.getParamValue(globalVar.uiParam, "secondGroupBoxTitle", "第二步：读取模型关键信息");
            this.groupBox6.Text = ParamManager.getParamValue(globalVar.uiParam, "thirdGroupBoxTitle", "第三步：配置公共参数");
            this.groupBox4.Text = ParamManager.getParamValue(globalVar.uiParam, "fourthGroupBoxTitle", "第四步：数据上传到数据库");
            this.groupBox2.Text = ParamManager.getParamValue(globalVar.uiParam, "reserveSet", "预留槽设置");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                dgv.Rows.Clear();
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.CheckFileExists = true;
                ofd.Multiselect = false;
                ofd.InitialDirectory = comMethod.dirPath;
                ofd.RestoreDirectory = true;
                ofd.Filter = "csv文件|*.csv";

                string path = "";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    path = ofd.FileName;
                    comMethod.dirPath = Path.GetDirectoryName(path);
                }
                else
                {
                    return;
                }

                pathTextBox.Text = path;
                List<CableContactItem> CCIList = comMethod.ReadCableContactItemExcel(pathTextBox.Text.Trim());

                for (int i = 0; i < CCIList.Count; i++)
                {

                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(this.dgv);
                    row.Cells[0].Value = string.IsNullOrEmpty(CCIList[i].Id) ? "" : CCIList[i].Id;
                    row.Cells[1].Value = string.IsNullOrEmpty(CCIList[i].cableStart) ? "" : CCIList[i].cableStart;
                    row.Cells[2].Value = string.IsNullOrEmpty(CCIList[i].cableNumber) ? "" : CCIList[i].cableNumber;
                    row.Cells[3].Value = string.IsNullOrEmpty(CCIList[i].cableEnd) ? "" : CCIList[i].cableEnd;
                    row.Cells[4].Value = string.IsNullOrEmpty(CCIList[i].cableType) ? "" : CCIList[i].cableType;
                    row.Cells[5].Value = string.IsNullOrEmpty(CCIList[i].cableUnit) ? "" : CCIList[i].cableUnit;
                    row.Cells[6].Value = string.IsNullOrEmpty(CCIList[i].cableLength) ? "" : CCIList[i].cableLength;


                    dgv.Rows.Add(row);
                }

            }
            catch (Exception ee)
            {
                MessageBox.Show("出现异常请联系管理员" + ee.Message, "温馨提示");
            }
        }

        private void btnInpute_Click(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists(pathTextBox.Text.Trim()))
                {
                    MessageBox.Show("文件不存在", "温馨提示");
                }

                List<CableContactItem> CCIList = comMethod.ReadCableContactItemExcel(pathTextBox.Text.Trim());

                for (int i = 0; i < CCIList.Count; i++)
                {

                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(this.dgv);
                    row.Cells[0].Value = string.IsNullOrEmpty(CCIList[i].Id) ? "" : CCIList[i].Id;
                    row.Cells[1].Value = string.IsNullOrEmpty(CCIList[i].cableStart) ? "" : CCIList[i].cableStart;
                    row.Cells[2].Value = string.IsNullOrEmpty(CCIList[i].cableNumber) ? "" : CCIList[i].cableNumber;
                    row.Cells[3].Value = string.IsNullOrEmpty(CCIList[i].cableEnd) ? "" : CCIList[i].cableEnd;
                    row.Cells[4].Value = string.IsNullOrEmpty(CCIList[i].cableType) ? "" : CCIList[i].cableType;
                    row.Cells[5].Value = string.IsNullOrEmpty(CCIList[i].cableUnit) ? "" : CCIList[i].cableUnit;
                    row.Cells[6].Value = string.IsNullOrEmpty(CCIList[i].cableLength) ? "" : CCIList[i].cableLength;


                    dgv.Rows.Add(row);
                }

            }
            catch (Exception ee)
            {
                MessageBox.Show("出现异常请联系管理员" + ee.Message, "温馨提示");
            }
        }

        private void tCodeBtn_Click(object sender, EventArgs e)
        {
            //if (twoRadioButton.Checked)
            //{
            //    TFCodeCMD.myCount = 2;
            //}
            //if (threeRadioButton.Checked)
            //{
            //    TFCodeCMD.myCount = 3;
            //}
            //TFCodeCMD.isTCode = true;
            //TFCodeCMDEvent.Raise();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            if (checkBoxSD.Checked)
            {

                dgv.ReadOnly = true;
                mainWidthTBox.ReadOnly = true;
                cableDiameterTBox.ReadOnly = true;
                csBottomHeightTBox.ReadOnly = true;
                csLayerSpaceTBox.ReadOnly = true;
                jsLayerSpaceTBox.ReadOnly = true;
                jsBottomHeightTBox.ReadOnly = true;

            }
            else
            {
                dgv.ReadOnly = false;
                dgv.ReadOnly = false;
                mainWidthTBox.ReadOnly = false;
                cableDiameterTBox.ReadOnly = false;
                csBottomHeightTBox.ReadOnly = false;
                csLayerSpaceTBox.ReadOnly = false;
                jsLayerSpaceTBox.ReadOnly = false;
                jsBottomHeightTBox.ReadOnly = false;
            }
            this.skinEngine1.SkinFile = comMethod.ReadSkinPath();
        }

        private void fCodeBtn_Click(object sender, EventArgs e)
        {
            TFCodeCMD.myCount = int.Parse(this.tbReserve.Text.Trim().ToString());
            TFCodeCMD.isTCode = false;
            TFCodeCMDEvent.Raise();
        }

        private void byqBtn_Click(object sender, EventArgs e)
        {
            // 获取选中行的索引
            int selectedRowIndex = dgv.SelectedCells[0].RowIndex;

            // 根据索引获取选中行的数据
            DataGridViewRow selectedRow = dgv.Rows[selectedRowIndex];

            // 获取选中行的数据
            if (selectedRow.Cells[2].Value == null)
            {
                MessageBox.Show("数据为空，请导入表格");
                return;
            }
            string value = selectedRow.Cells[2].Value.ToString(); // 第三列数据 变压器编码

            BYQCodeCMD.code = value;
            BYQCodeCMDEvent.Raise();

            BYQCodeCMD.action = new Action<List<ConnectorItem>>((result) =>
                 {
                     foreach (DataGridViewRow row in dgv.SelectedRows)
                     {
                         row.Cells[7].Value = JsonConvert.SerializeObject(result);
                     }

                 });
        }

        private void threeRadioButton_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void reservedBtn_Click(object sender, EventArgs e)
        {
            YLCCodeCMD.count = int.Parse(this.tbReserve.Text.Trim().ToString());
            YLCCodeCMDEvent.Raise();
            YLCCodeCMD.action = new Action<List<XYZ>>((result) =>
            {
                foreach (DataGridViewRow row in dgv.SelectedRows)
                {
                    row.Cells[8].Value = JsonConvert.SerializeObject(comMethod.RoundXYZList(result));
                }

            });
        }

        private void supportBtn_Click(object sender, EventArgs e)
        {
            ZJCCodeCMDEvent.Raise();
            ZJCCodeCMD.action = new Action<List<XYZ>>((Result) =>
            {

                foreach (DataGridViewRow row in dgv.SelectedRows)
                {
                    row.Cells[9].Value = JsonConvert.SerializeObject(comMethod.RoundXYZList(Result));
                }

            });

        }

        private void jiguiBtn_Click(object sender, EventArgs e)
        {
            // 获取选中行的索引
            int selectedRowIndex = dgv.SelectedCells[0].RowIndex;

            // 根据索引获取选中行的数据
            DataGridViewRow selectedRow = dgv.Rows[selectedRowIndex];

            // 获取选中行的数据
            if (selectedRow.Cells[3].Value == null)
            {
                MessageBox.Show("数据为空，请导入表格");
                return;
            }
            string value = selectedRow.Cells[3].Value.ToString(); // 第三列数据 变压器编码

            JGCodeCMD.code = value;
            JGCodeCMDEvent.Raise();
            JGCodeCMD.action = new Action<List<ConnectorItem>>((result) =>
            {

                foreach (DataGridViewRow row in dgv.SelectedRows)
                {
                    row.Cells[10].Value = JsonConvert.SerializeObject(result);
                }

            });
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            SaveFileDialog ofd = new SaveFileDialog();
            ofd.InitialDirectory = comMethod.dirPath;
            ofd.RestoreDirectory = true;
            ofd.Filter = "ini文件|*.ini";

            string path = "";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path = ofd.FileName;
                comMethod.dirPath = Path.GetDirectoryName(path);
            }
            else
            {
                return;
            }
            List<CableContactItemSave> CCISList = new List<CableContactItemSave>();
            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (!row.IsNewRow) // 忽略新行的模板
                {
                    CableContactItemSave ccis = new CableContactItemSave();
                    ccis.Id = row.Cells[0].Value == null ? "" : row.Cells[0].Value.ToString();
                    ccis.cableStart = row.Cells[1].Value == null ? "" : row.Cells[1].Value.ToString();
                    ccis.cableNumber = row.Cells[2].Value == null ? "" : row.Cells[2].Value.ToString();
                    ccis.cableEnd = row.Cells[3].Value == null ? "" : row.Cells[3].Value.ToString();
                    ccis.cableType = row.Cells[4].Value == null ? "" : row.Cells[4].Value.ToString();
                    ccis.cableUnit = row.Cells[5].Value == null ? "" : row.Cells[5].Value.ToString();
                    ccis.cableLength = row.Cells[6].Value == null ? "" : row.Cells[6].Value.ToString();
                    ccis.BYQConnItemList = row.Cells[7].Value == null ? "" : row.Cells[7].Value.ToString();
                    ccis.YLCxyzList = row.Cells[8].Value == null ? "" : row.Cells[8].Value.ToString();
                    ccis.ZJxyzList = row.Cells[9].Value == null ? "" : row.Cells[9].Value.ToString();
                    ccis.JGConnItemList = row.Cells[10].Value == null ? "" : row.Cells[10].Value.ToString();
                    CCISList.Add(ccis);
                }
            }
            CommonConfig cc = new CommonConfig();
            cc.MainWidth = mainWidthTBox.Text.Trim();
            cc.JXMainWidth=mainWidthTBox.Text.Trim();
            cc.KXMainWidth=textBox6.Text.Trim();
            cc.Diameter = cableDiameterTBox.Text.Trim();
            cc.CSBottomHeight = csBottomHeightTBox.Text.Trim();
            cc.CSLayerSpace = csLayerSpaceTBox.Text.Trim();
            cc.JSBottomHeight = jsBottomHeightTBox.Text.Trim();
            cc.JSLayerSpace = jsLayerSpaceTBox.Text.Trim();

            SaveConfig saveConfig = new SaveConfig();
            saveConfig.CommonConfig = cc;
            saveConfig.ccisList = CCISList;
            FileCodeCMDEvent.Raise();
            FileCodeCMD.action = new Action<string>((result) =>
            {
                saveConfig.FileName = result;
                comMethod.FileName = result;
                comMethod.SaveConfig(path, saveConfig);
            });
           



        }

        private void readBtn_Click(object sender, EventArgs e)
        {
            try
            {
                dgv.Rows.Clear();
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.CheckFileExists = true;
                ofd.Multiselect = false;
                ofd.InitialDirectory = comMethod.dirPath;
                ofd.RestoreDirectory = true;
                ofd.Filter = "ini文件|*.ini";

                string path = "";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    path = ofd.FileName;
                    comMethod.dirPath = Path.GetDirectoryName(path);
                }
                else
                {
                    return;
                }

                SaveConfig saveConfig = comMethod.ReadConfig(path);
                List<CableContactItemSave> CCISList = saveConfig.ccisList;

                for (int i = 0; i < CCISList.Count; i++)
                {

                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(this.dgv);
                    row.Cells[0].Value = string.IsNullOrEmpty(CCISList[i].Id) ? "" : CCISList[i].Id;
                    row.Cells[1].Value = string.IsNullOrEmpty(CCISList[i].cableStart) ? "" : CCISList[i].cableStart;
                    row.Cells[2].Value = string.IsNullOrEmpty(CCISList[i].cableNumber) ? "" : CCISList[i].cableNumber;
                    row.Cells[3].Value = string.IsNullOrEmpty(CCISList[i].cableEnd) ? "" : CCISList[i].cableEnd;
                    row.Cells[4].Value = string.IsNullOrEmpty(CCISList[i].cableType) ? "" : CCISList[i].cableType;
                    row.Cells[5].Value = string.IsNullOrEmpty(CCISList[i].cableUnit) ? "" : CCISList[i].cableUnit;
                    row.Cells[6].Value = string.IsNullOrEmpty(CCISList[i].cableLength) ? "" : CCISList[i].cableLength;
                    row.Cells[7].Value = string.IsNullOrEmpty(CCISList[i].BYQConnItemList) ? "" : CCISList[i].BYQConnItemList;
                    row.Cells[8].Value = string.IsNullOrEmpty(CCISList[i].YLCxyzList) ? "" : CCISList[i].YLCxyzList;
                    row.Cells[9].Value = string.IsNullOrEmpty(CCISList[i].ZJxyzList) ? "" : CCISList[i].ZJxyzList;
                    row.Cells[10].Value = string.IsNullOrEmpty(CCISList[i].JGConnItemList) ? "" : CCISList[i].JGConnItemList;

                    dgv.Rows.Add(row);
                }

                CommonConfig cc = saveConfig.CommonConfig;
                mainWidthTBox.Text = cc.MainWidth == null ? "" : cc.MainWidth;
                
                //新增馈线侧的道宽
                textBox6.Text = cc.KXMainWidth == null ? "" : cc.KXMainWidth;
                
                cableDiameterTBox.Text = cc.Diameter == null ? "" : cc.Diameter;
                csBottomHeightTBox.Text = cc.CSBottomHeight == null ? "" : cc.CSBottomHeight; ;
                csLayerSpaceTBox.Text = cc.CSLayerSpace == null ? "" : cc.CSLayerSpace; ;
                jsBottomHeightTBox.Text = cc.JSBottomHeight == null ? "" : cc.JSBottomHeight; ;
                jsLayerSpaceTBox.Text = cc.JSLayerSpace == null ? "" : cc.JSLayerSpace; ;

            }
            catch (Exception ee)
            {
                MessageBox.Show("出现异常请联系管理员" + ee.Message, "温馨提示");
            }
        }

        private void btnMainWidth_Click(object sender, EventArgs e)
        {
            WidthCodeCMDEvent.Raise();
            WidthCodeCMD.action = new Action<List<xXYZ>>((result) =>
            {
                mainWidthTBox.Text = JsonConvert.SerializeObject(comMethod.RoundXYZList(comMethod.xXYZ2XYZ(result)));
            });
        }

        private void checkBoxSD_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxSD.Checked)
            {

                dgv.ReadOnly = true;
                mainWidthTBox.ReadOnly = true;
                cableDiameterTBox.ReadOnly = true;
                csBottomHeightTBox.ReadOnly = true;
                csLayerSpaceTBox.ReadOnly = true;
                jsLayerSpaceTBox.ReadOnly = true;
                jsBottomHeightTBox.ReadOnly = true;

                //馈线侧
                textBox6.ReadOnly= true;
               

            }
            else
            {
                dgv.ReadOnly = false;
                dgv.ReadOnly = false;
                mainWidthTBox.ReadOnly = false;
                cableDiameterTBox.ReadOnly = false;
                csBottomHeightTBox.ReadOnly = false;
                csLayerSpaceTBox.ReadOnly = false;
                jsLayerSpaceTBox.ReadOnly = false;
                jsBottomHeightTBox.ReadOnly = false;

                //馈线侧
                textBox6.ReadOnly = false;
               
            }
        }

        private void downloadBtn_Click(object sender, EventArgs e)
        {
            string path = @"C:\ProgramData\Autodesk\Revit\Addins\2020\whdqh\电缆联系表.csv";
            try
            {
                if (File.Exists(path))
                {
                    string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\电缆联系表.csv";
                    File.Copy(path, desktopPath, true);

                }


            }
            catch (Exception)
            {


            }
           
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                #region 读取数据
                List<CableContactItemSave> CCISList = new List<CableContactItemSave>();
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    if (!row.IsNewRow) // 忽略新行的模板
                    {
                        CableContactItemSave ccis = new CableContactItemSave();
                        ccis.Id = row.Cells[0].Value == null ? "" : row.Cells[0].Value.ToString();
                        ccis.cableStart = row.Cells[1].Value == null ? "" : row.Cells[1].Value.ToString();
                        ccis.cableNumber = row.Cells[2].Value == null ? "" : row.Cells[2].Value.ToString();
                        ccis.cableEnd = row.Cells[3].Value == null ? "" : row.Cells[3].Value.ToString();
                        ccis.cableType = row.Cells[4].Value == null ? "" : row.Cells[4].Value.ToString();
                        ccis.cableUnit = row.Cells[5].Value == null ? "" : row.Cells[5].Value.ToString();
                        ccis.cableLength = row.Cells[6].Value == null ? "" : row.Cells[6].Value.ToString();
                        ccis.BYQConnItemList = row.Cells[7].Value == null ? "" : row.Cells[7].Value.ToString();
                        ccis.YLCxyzList = row.Cells[8].Value == null ? "" : row.Cells[8].Value.ToString();
                        ccis.ZJxyzList = row.Cells[9].Value == null ? "" : row.Cells[9].Value.ToString();
                        ccis.JGConnItemList = row.Cells[10].Value == null ? "" : row.Cells[10].Value.ToString();
                        CCISList.Add(ccis);
                    }
                }
                CommonConfig cc = new CommonConfig();
                cc.MainWidth = mainWidthTBox.Text.Trim();
                
                //新增进线侧和馈线侧的道宽
                cc.JXMainWidth=mainWidthTBox.Text.Trim();
                cc.KXMainWidth=textBox6.Text.Trim();
                
                cc.Diameter = cableDiameterTBox.Text.Trim();
                cc.CSBottomHeight = csBottomHeightTBox.Text.Trim();
                cc.CSLayerSpace = csLayerSpaceTBox.Text.Trim();
                cc.JSBottomHeight = jsBottomHeightTBox.Text.Trim();
                cc.JSLayerSpace = jsLayerSpaceTBox.Text.Trim();

                SaveConfig saveConfig = new SaveConfig();
                saveConfig.CommonConfig = cc;
                saveConfig.ccisList = CCISList;
                #endregion
                if (string.IsNullOrEmpty(comMethod.FileName))
                {
                    MessageBox.Show("请先保存后再上传");
                    return;
                }
                saveConfig.FileName = comMethod.FileName;
               
                    using (HttpClient httpClient = new HttpClient())
                    {

                        string jsonData = JsonConvert.SerializeObject(saveConfig);
                        // 发送请求数据包
                        StringContent content = new StringContent(jsonData, Encoding.UTF8);
                        // 设置HTTP 响应上的ContentType --application/json
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        // 请求访问地址
                        string url = comMethod.ReadAPIPath();
                        // 发出HTTP的Post请求
                        HttpResponseMessage response = await httpClient.PostAsync(url, content);
                        // 读取返回结果
                        string responseContent = await response.Content.ReadAsStringAsync();
                        // 将字符转对象
                        ResponseResult<string> result = JsonConvert.DeserializeObject<ResponseResult<string>>(responseContent);
                        MessageBox.Show(result.Message);

                    }
               
            }
            catch (Exception)
            {
                MessageBox.Show("发生异常上传失败，请插件数据是否正确或者网络是否通畅");
            }

        }

        private void APIBtn_Click(object sender, EventArgs e)
        {
            APIForm form = new APIForm();   
            form.ShowDialog();
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // 从根目录读取文件
                string filePath = "E:\\Project data\\布线\\自动布线\\RevitAddin\\bin\\Debug\\requestData.txt";  // 文件路径

                if (!File.Exists(filePath))
                {
                    MessageBox.Show("请求文件不存在", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // 读取文件内容
                string[] fileLines = File.ReadAllLines(filePath);

                // 构造请求参数对象
                RequestPara requestPara = new RequestPara
                {
                    FileName = fileLines[0].Split('=')[1].Trim(),  // 第一行是文件名
                    ACList = new List<AutoConfig>()
                };

                // 从第二行开始解析请求参数
                for (int i = 1; i < fileLines.Length; i++)
                {
                    string[] parameters = fileLines[i].Split(',');

                    AutoConfig config = new AutoConfig
                    {
                        cableStart = parameters[0].Split('=')[1].Trim(),
                        cableNumber = parameters[1].Split('=')[1].Trim(),
                        cableEnd = parameters[2].Split('=')[1].Trim(),
                        isTop=bool.Parse(parameters[3].Split('=')[1].Trim()),
                        isOneTwo = bool.Parse(parameters[4].Split('=')[1].Trim()),
                        IsOpposite = bool.Parse(parameters[5].Split('=')[1].Trim())
                    };

                    requestPara.ACList.Add(config);
                }

                // 使用 HttpClient 发出 HTTP Post 请求
                using (HttpClient httpClient = new HttpClient())
                {
                    // 将请求参数对象序列化为 JSON 格式
                    string jsonData = JsonConvert.SerializeObject(requestPara);//请求参数对象

                    // 发送请求数据包
                    StringContent content = new StringContent(jsonData, Encoding.UTF8);

                    // 设置 HTTP 请求的 Content-Type 为 application/json
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    // 设置请求的访问地址
                    string url = "http://127.0.0.1:5001/AutoWiring/GetData"; // 从配置或方法读取 API 路径

                    // 发出 HTTP Post 请求
                    HttpResponseMessage response = await httpClient.PostAsync(url, content);

                    // 读取返回的响应内容
                    string responseContent = await response.Content.ReadAsStringAsync();

                    // 将响应内容反序列化为对象
                    ResponseResult<Dictionary<string, Dictionary<string, List<XYZ>>>> result = JsonConvert.DeserializeObject<ResponseResult<Dictionary<string, Dictionary<string, List<XYZ>>>>>(responseContent);

                    // 返回反序列化后的结果并在界面上显示
                    if (result != null && result.Code == HttpStatusCode.OK)
                    {
                        MessageBox.Show($"操作成功: {result.Message}", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show($"操作失败: {result?.Message ?? "无效响应"}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理异常并在界面上显示错误信息
                MessageBox.Show($"请求异常: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            WidthCodeCMDEvent.Raise();
            WidthCodeCMD.action = new Action<List<xXYZ>>((result) =>
            {
                textBox6.Text = JsonConvert.SerializeObject(comMethod.RoundXYZList(comMethod.xXYZ2XYZ(result)));
            });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CableDataForm cableDataForm = new CableDataForm();
            cableDataForm.ShowDialog();
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }


}


